<template>
	<view class="has-mg-15">
		<view class="is-input has-pdlr-20 is-size-20">
			<text class="has-pdr-20" v-if="bank == undefined || bank.length <= 0">银行</text>
			<view class="has-bg-red is-txt-inline has-pdlr-15" v-if="bank ==''" @tap="addbank">
				<uni-icons class="is-txt-inline has-mglr-12" type="card" color="#FFFFFF" size="24"></uni-icons>				
				请先添加银行卡
			</view>
			<view  v-if="bank !==undefined && bank.length > 0" class="has-bg-red is-txt-inline has-pdlr-10 is-block">
				<picker class="is-size-16" :value="index" @change="bindPickerChange" :range="bank" :range-key="'name'">
					<view class="uni-input">
						<uni-icons class="is-txt-inline" type="card" color="#FFFFFF" size="18"></uni-icons>
					{{bank[index].name}}</view>
				</picker>
			</view>
		</view>
		<view class="is-input has-pdlr-20 is-size-20">
			<text class="has-pdr-20">金额</text><text class="has-floatr is-size-12 is-grey">卖币手续费为 {{fox_user_cash*100}}%</text>
			<input type="number" @input="onInput_money" placeholder="请输入金额" />
			<text>DC</text>
		</view>
		<view class="has-mgt-30 has-pd-20 is-size-14">
			<text class="is-blue-color is-bold-no">单笔卖币限额 ￥{{fox_money_sell_low}}-{{fox_money_sell_top}}</text>
		</view>
		<view class="has-mglr-20">
				<button type="primary" @tap="buyit">提交</button>
		</view>
		<view class="has-mgt-20 has-pdlr-20 is-size-14 is-bold-no">
			<view class="is-red-color">
				当前可出售DC数量：DC{{user_money_can}}
			</view>
		</view>
	</view>
</template>

<script>
	var _self;
	import uniIcons from "@/components/uni-icons/uni-icons.vue"
	export default {
	    components: {uniIcons},
	    data() {
	        return {
				foxtoken:'',
	            title: 'picker',
	            bank: [],
	            index: 0,
				bank_id:'',
				userinfo:[],
				money:0,
				fox_user_cash:0,
				fox_money_sell_low:0,
				fox_money_sell_top:0,
				user_money_can:0
			}
		},
		onLoad(){
			_self = this;
			this.get_bank()
		},
		methods: {
			bindPickerChange: function(e) {
				this.index = e.target.value
				this.bank_id = this.bank[e.target.value].id
				// console.log(this.bank_id)
			},
			onInput_money(e) {
				this.money = e.detail.value;
			},
			get_bank(){
				let info = this.service.getfox();
				if(info[0]){
					this.foxtoken = info[0]['foxtoken'];
					uni.request({
						url: this.service.FOX_URL + 'diamond/get_userinfo_bank',
						method:'POST',
						data: {
							user_key: this.foxtoken
						},
						dataType:'json',
						header:{
							'content-type':'application/x-www-form-urlencoded',
						},
						success: res => {
							// var rest = JSON.stringify(res);
							// console.log(rest)
							var rest = res.data;
							if(rest.status == 'success'){
								this.userinfo = rest.msg;
								this.bank = this.userinfo['bank'];
								this.user_money_can = this.userinfo['user_money_can'];
								this.fox_user_cash = this.userinfo['diamondset']['diamond_user_cash'];
								this.fox_money_sell_low = this.userinfo['diamondset']['diamond_money_sell_low'];
								this.fox_money_sell_top = this.userinfo['diamondset']['diamond_money_sell_top'];
								if(this.bank){
									let one = this.bank[this.index];
									this.bank_id = one.id;
									// console.log(this.bank_id)
								}
							}else{
								uni.showToast({
									title:rest.msg,
									icon:'none',
									mask:true
								})
							}
						}
					})
				}else{
					uni.reLaunch({
						url:'../logon/login'
					})
				}
			},
			addbank(){
				uni.navigateTo({
					url:'../bank/bank_list'
				})
			},
			buyit(){
				let money = parseInt(this.money);
				let user_key = this.foxtoken;
				let bank_id = this.bank_id;
				if(bank_id =='' || bank_id === undefined || bank_id.length == 0){
					uni.showToast({
						title:'银行卡选择错误',
						icon:'none',
						mask:true
					})
					return;
				}
				if(money =='' || money == 0){
					uni.showToast({
						title:'金额输入错误',
						icon:'none',
						mask:true
					})
					return;
				}
				if(money < this.fox_money_sell_low){
					uni.showToast({
						title:'输入金额不能小于 '+this.fox_money_sell_low,
						icon:'none',
						mask:true
					})
					return;
				}
				if(money > this.fox_money_sell_top){
					uni.showToast({
						title:'输入金额不能大于 '+this.fox_money_sell_top,
						icon:'none',
						mask:true
					})
					return;
				}
				uni.request({
					url: this.service.FOX_URL + 'diamond/add_user_money_cash',
					method:'POST',
					data: {
						user_key: user_key,
						bank_id:bank_id,
						money:money
					},
					dataType:'json',
					header:{
						'content-type':'application/x-www-form-urlencoded',
					},
					success: res => {
						// var rest = JSON.stringify(res);
						// console.log(res)
						var rest = res.data;
						if(rest.status == 'success'){
							let new_id = rest.msg;
							uni.navigateTo({
								url:'./selldo?id='+new_id
							})
						}else{
							uni.showToast({
								title:rest.msg,
								icon:'none',
								mask:true
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
	.is-input{
		height: 88rpx;
		line-height: 88rpx;
		margin-bottom: 20rpx;
	}
	.is-input text{		
		display: inline-block;
		margin-right: 10rpx;
		color: #222;
	}
	.is-input input{
		display: inline-block;
		height: 88rpx;
		line-height: 88rpx;
		border-bottom: 1px solid #D0DEE5;
	}
</style>
